package T41_T50;

import datastructure.TreeNode;

public class T43 {
    //先序遍历判断是否属于搜索树
    public boolean isValidBST(TreeNode root){
        return check(root,Long.MIN_VALUE,Long.MAX_VALUE);
    }
    private boolean check(TreeNode node,long left,long right){
        if (node==null)return true;
        long nodeValue=node.val;
        return nodeValue<right&&nodeValue>left&&
                check(node.right,nodeValue,right)&&
                check(node.left,left,nodeValue);
    }
    /*//中序遍历判断是否属于搜索二叉树
    long pre=Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if (root==null)return true;
        //判断左子树是否符合条件
        if (!isValidBST(root.left)||root.val<pre)return false;
        pre=root.val;
        return isValidBST(root.right);
    }*/

}
